package com.ntschy.common.valid.datetime;


import cn.hutool.core.util.StrUtil;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

/**
 * @Author: Ezreal_geng
 * @Description: 日期时间格式校验
 * @Date:Create：2020-11-22 15:52
 * @Modified By：
 */
public class DateTimeValidation implements ConstraintValidator<DateTime, String> {
    boolean must;

    @Override
    public void initialize(DateTime constraintAnnotation) {
        must = constraintAnnotation.must();
    }

    @Override
    public boolean isValid(String value, ConstraintValidatorContext constraintValidatorContext) {
        if(!must && value == null) {
            return true;
        } else {
            if (StrUtil.isEmpty(value)) {
                return false;
            } else {
                return DateTimeValidationUtil.isDateTime(value);
            }
        }
    }
}